package com.ruoyi.web.controller.teaching;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.teaching.domain.PlatformPComment;
import com.ruoyi.teaching.service.CheckService;
import com.ruoyi.teaching.service.PlatformPCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王旭
 * @since 2024-07-01
 */
@RestController
@RequestMapping("/platform/comment")
@Api(tags = "platform-评论管理")
@Validated
public class  PlatformPCommentController {
    @Autowired
    private PlatformPCommentService platformPCommentService;

    @Autowired
    private CheckService checkService;

    /**
     * 查询某个项目下的所有评论
     */
//    @PreAuthorize("@ss.hasPermi('platform:comment:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询某个项目下的所有评论",notes = "查询某个项目下的所有评论")
    public AjaxResult list(Integer pageNum, Integer pageSize, Integer commentType, Integer commentProjectId)
    {
        return platformPCommentService.selectPlatformPCommentList(pageNum, pageSize, commentType, commentProjectId);
    }

    /**
     * 新增评论
     */
//    @PreAuthorize("@ss.hasPermi('platform:comment:add')")
    @Log(title = "【新增评论】", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增评论", notes = "新增评论")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody PlatformPComment platformPComment)
    {
        //一人只能评论一个项目一次的校验
        if(checkService.checkComment(platformPComment)){
            return AjaxResult.error("该"+ (platformPComment.getCommentType() == 0 ? "课程":"虚拟仿真") +"您已评价，无需重复评价");
        }
        return platformPCommentService.insertPlatformPComment(platformPComment);
    }

    /**
     * 修改评论
     */
//    @PreAuthorize("@ss.hasPermi('platform:comment:edit')")
    @Log(title = "【修改评论】", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改评论", notes = "修改评论")
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformPComment platformPComment)
    {
        return platformPCommentService.updatePlatformPComment(platformPComment);
    }

    /**
     * 删除评论
     */
//    @PreAuthorize("@ss.hasPermi('platform:comment:remove')")
    @Log(title = "【删除评论】", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除评论", notes = "删除评论")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return platformPCommentService.deletePlatformPCommentByIds(ids);
    }
}

